package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelCommentService travelCommentService;

    /*
    * 根据游记的id获取游记内容
    * 资源:/travels/{id}
    * 参数:
    * */
    @GetMapping("/{id}")
    public Object getTravels(@PathVariable Long id){
        Travel travel = travelService.get(id);
        return travel;
    }

    /*
    * 获取所有的已发布公开的游记
    * 资源:/travels
    * 动作:GET
    * 参数:isPublic=true&state=2
    * */
    @GetMapping
    public Object Travels(TravelQueryObject qo){
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_RELEASE);
        return travelService.queryForTravels(qo);
    }

    /*
     * 获取每周/每月推荐游记
     * 资源:/travels/commends
     * 动作:GET
     * 参数:
     * */
    @GetMapping("/commends")
    public Object TravelCommends(@RequestParam Integer type){

        return travelCommendService.selectCommend(type);
    }

    //查询一条点评中所有的评论,并且回显在页面
    @GetMapping("/{travelId}/comment")
    public Object getCommentreplay(TravelCommentQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = travelCommentService.selectForList(qo);
        return pageInfo;
    }

    //查询评中评的总条数
    @GetMapping("/{id}/countId")
    public Object getCountId(@PathVariable Long id){
        return travelCommentService.selectCountById(id);
    }

    //添加一条评论
    @PostMapping("/travelcomment/{travelId}")
    public Object saveReplayComment(String content,Long userId,@PathVariable Long travelId){
        JSONResult jsonResult = new JSONResult();
        Travel travel= new Travel();
        TravelComment travelComment = new TravelComment();
        travel.setId(travelId);
        travelComment.setTravel(travel);
        User user = new User();
        user.setId(userId);
        travelComment.setUser(user);
        travelComment.setContent(content);
        try {
            travelCommentService.saveComment(travelComment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /*
     * 根据游记的id修改状态collect为收藏
     * 资源:/travels/{id}
     * 动作:PUT
     * 参数:collect=1/0
     * */
    @PutMapping("/{id}")
    public Object updateCollectById(@PathVariable Long id, Integer collect, Long userId){
        travelService.updateCollectById(id, collect, userId);
        return new JSONResult();
    }
}
